package com.fms.data.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;

import junit.framework.TestCase;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jmock.Mockery;

import com.fms.model.domain.FlowSheet;

public class FlowSheetOperationsTest extends TestCase
{
private Mockery mock;

	
	public void setUp()
	{
		mock = new Mockery();
	}
	
	
	public void tearDown()
	{
		mock.assertIsSatisfied();
	}
	
	public void testExceptionThrownWhenFileExists()
	{
		FlowSheetOperations ops = new FlowSheetOperations();
		
		File f = new File("testData/testExceptionThrownWhenFileExists.xls");
		if(!f.exists())
		{
			try
			{
				f.createNewFile();
			}
			catch (IOException e)
			{
				fail(e.getMessage());
			}
		}
		
		try
		{
			ops.createTemplate(new FlowSheet("testData/testExceptionThrownWhenFileExists.xls"));
			fail("Should have thrown");
		} 
		catch (FileException e)
		{
			stringCheck(e.getMessage(), "Could not create the template: The file testExceptionThrownWhenFileExists.xls already exists in that location.");
		}
		
		
	}
	
	public void testExceptionThrownWhenTemplateIsMissing()
	{
		FlowSheetOperations ops = new FlowSheetOperations("template/notHere.xls");
		
		File f = new File("testData/testExceptionThrownWhenTemplateIsMissing.xls");
		if(f.exists())
		{
			f.delete();
		}
		
		try
		{
			ops.createTemplate(new FlowSheet("testData/testExceptionThrownWhenTemplateIsMissing.xls"));
			fail("Should have thrown");
		} 
		catch (FileException e)
		{
			stringCheck(e.getMessage(),"Could not create the template: The template, notHere.xls does not exist!");
		}
		
		
	}
	
	public void testCheckDateInformationPrintedCorrectly()
	{
		FlowSheetOperations ops = new FlowSheetOperations();
		
		File f = new File("testData/testCheckDateInformationPrintedCorrectly.xls");
		if(f.exists())
		{
			f.delete();
		}
		FlowSheet s = new FlowSheet("testData/testCheckDateInformationPrintedCorrectly.xls");
		Calendar c = Calendar.getInstance();
		
		c.set(Calendar.YEAR, 2000);
		c.set(Calendar.MONTH, 2);
		c.set(Calendar.DAY_OF_MONTH, 15);
		c.set(Calendar.HOUR_OF_DAY, 22);
		c.set(Calendar.MINUTE, 15);
		s.setDate(c);
		try
		{
			ops.createTemplate(s);
		} 
		catch (FileException e)
		{
			fail("Should have passed!" + e.getMessage());
		}
		
		
		HSSFWorkbook book = null;
		try
		{
			book = new HSSFWorkbook(new FileInputStream(s.getFileName()));
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		stringCheck(getCell(book, 2, 8), "Start Time: 10:15PM");
		stringCheck(getCell(book, 3, 8), "Stop Time: 1:00AM");
		
		

		stringCheck(getCell(book, 21, 0), "10:20PM");
		stringCheck(getCell(book, 22, 0), "10:50PM");
		stringCheck(getCell(book, 23, 0), "11:20PM");
		stringCheck(getCell(book, 24, 0), "11:50PM");
		stringCheck(getCell(book, 25, 0), "12:20AM");
		
		
	}
	
	public void testExistsForOperations()
	{
		FlowSheetOperations ops = new FlowSheetOperations("template/notHere.xls");
		
		File f = new File("testData/testExistsForOperations.xls");
		if(f.exists())
		{
			f.delete();
		}
		assertFalse(ops.exists(new FlowSheet("testData/testExistsForOperations.xls")));
		
		try
		{
			f.createNewFile();
		} 
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		assertTrue(ops.exists(new FlowSheet("testData/testExistsForOperations.xls")));
	}
	
	private void stringCheck(String source, String checkAgainst)
	{
		if(!source.trim().equals(checkAgainst.trim()))
		{			
			fail("The string source:\n" + source + "\nDid not match the check:\n" + checkAgainst);		
		}
	}
	
	@SuppressWarnings("deprecation")
	private String getCell(HSSFWorkbook w, int row, int col)
	{
		return w.getSheetAt(0).getRow(row).getCell(col).getStringCellValue();
		
		
	}
}
